/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.topic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.entity.TopicContent;
import org.grayrabbit.cms.service.topic.TopicContentService;
import org.grayrabbit.cms.vo.topic.TopicContentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>TopicContentAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-06-02 12:39:24 
 */
@Controller
@Scope("prototype")
public class TopicContentAction extends GenericAction<TopicContentBean> {
	private static final long serialVersionUID = -1L;
	//Service接口接入
	@Autowired
	@Qualifier("topicContentServiceImpl")
	private TopicContentService topicContentService;
	
	//VO对象，由前端页面数据组装
	private TopicContentBean topicContent;
	
	private String ids;
	
	
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	/**return 前端页面数据组装VO对象,<b>TopicContentBean</b>*/
	public TopicContentBean getTopicContent() {
		return topicContent;
	}

	public void setTopicContent(TopicContentBean topicContent) {
		this.topicContent = topicContent;
	}
	
	//分页对象
	private Page<TopicContent> page = new Page<TopicContent>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<TopicContent> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<TopicContent> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(topicContent==null){
			topicContent = new TopicContentBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public TopicContentBean getModel() {
		return topicContent;
	}
	
	@Override
	protected void prepareModel(){
		if(StringUtils.isNotBlank(topicContent.getTopicContentId())){
			topicContent = (TopicContentBean)dozer.map(topicContentService.getTopicContent(topicContent.getTopicContentId()),TopicContentBean.class);
		}
		
	}

	/**
	 * 批量操作，ID处理
	 * @return
	 */
	private String[] getBatchIds(){
		String[] idBatchs=null;
		if((null!=ids)&&(StringUtils.isNotBlank(ids))){
			idBatchs=ids.split("@");
		}
		return idBatchs;
	}
	
	/**
	 * 删除<b>TopicContent</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到topiccontent-list.action
	 */
	@Override
	@Action(value="topiccontent-delete")
	public String delete(){
		String text = "";
		String[] idBatchs=getBatchIds();
		String topicId = topicContent.getTopic().getTopicId();
		List<TopicContent> list = topicContentService.findByTopicId(topicId);
		if(list.size()== 0){
			text = "no";
		}
		try{
			for(int i=0;i<list.size();i++){
				for(int j=0;j<idBatchs.length;j++){
					if(list.get(i).getContent().getContentId().equals(idBatchs[j])){
						TopicContent tc = topicContentService.getTopicContent(list.get(i).getTopicContentId());
						topicContentService.delete(tc);
						text = "yes";
					}
				}
			}
		}catch(Exception e){
			text = "no";
		}
		
		return renderText(text);
	}
	
	/**
	 * TopicContent的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到topiccontent-list.jsp页面
	 */
	@Override
	@Action(value="topiccontent-list")
	public String list(){
		topicContentService.list(page,topicContent);
		return SUCCESS;
	}
	/**
	 * TopicContent的新增与编辑的方法执行成功能
	 * 将映射到topiccontent-input.jsp
	 */
	@Action(value="topiccontent-input")
	public String input(){
		prepareModel();
		return SUCCESS;
	}
	
	
	/**
	 * save or update TopicContent的方法，将成员property topicContent转成TopicContent实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到topiccontent-list.action
	 */
	@Override
	@Action(value="topiccontent-save")
	public String save(){
		String text = "";
		String[] idBatchs=getBatchIds();
		String topicId = topicContent.getTopic().getTopicId();
		List<TopicContent> list = topicContentService.findByTopicId(topicId);
		try{
			for(int i=0;i<idBatchs.length;i++){
				TopicContent obj = (TopicContent) dozer.map(topicContent, TopicContent.class);
				if(!StringUtils.isNotBlank(obj.getTopicContentId())){
					obj.setTopicContentId(null);
				}
				for(int j=0;j<list.size();j++){
					if(list.get(j).getContent().getContentId().equals(idBatchs[i])){
						text = "no";
					}
				}
				if(!text.equals("no")){
					obj.setContent(new Content(idBatchs[i]));
					obj.setTopic(new Topic(topicId));
					topicContentService.save(obj);
					text = "yes";
				}
			}
		}catch(Exception e){
			text = "no";
		}
		
		return renderText(text);
	}
	
}